package com.suning.app.snpaasadmin.action;

import com.opensymphony.xwork2.ActionSupport;
import com.suning.app.snpaasadmin.api.LoginApi;
import com.suning.app.snpaasadmin.operation.CfClusterCtrlmachineDBOperation;
import com.suning.app.snpaasadmin.pojo.CfClusterCtrlmachine;
import com.suning.app.snpaasadmin.util.ClusterClientUtil;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.http.HttpHost;
import org.apache.http.protocol.HttpContext;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class LoginAction extends ActionSupport {

    public static final String USERNAME = "username";
    public static final String PASSWORD = "password";
    public static final String USERSTATE = "userstate";
    public static final String HOST = "host";
    public static final String ACTIVE = "active";
    public static final String INACTIVE = "inactive";
    public static final String BOSHBASICAUTH = "BoshBasicAuth";
    public static final String BOSHAUTHHOST = "boshauthhost";
    public static final String CFBASICAUTH = "cfbasicauth";
    public static final String CFAUTHHOST = "cfauthhost";
    private static final long serialVersionUID = 1L;
    private String host;
    private String userName;
    private String passwd;
    private Map<String, Object> dataMap;
    private Logger logger = LoggerFactory.getLogger(LoginAction.class);
    
    @Autowired
    private CfClusterCtrlmachineDBOperation machineDB;
    
    
    /**
     * 
     * 功能描述: <br>
     * 用户登录 post Method
     *
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public String login() {
        dataMap = new HashMap<String, Object>();
        try {
            HttpHost httpHost = new HttpHost(host, 25555, "https");
            HttpContext cfContext = ClusterClientUtil.createBasicAuthContext(userName, passwd, httpHost);
            Map<String, Object> result = LoginApi.CheckLogin(cfContext, httpHost);
            if (result.get("user") != null) {
                CfClusterCtrlmachine cm = new CfClusterCtrlmachine();
                cm.setHostIp(host);
                cm.setPassword(passwd);
                cm.setUserName(userName);
                cm.setState("active");
                
                //判断数据库中是否有该IP
                if(machineDB.selectIpUser(host, userName)) {
                    machineDB.insert(cm);
                }
                /*HttpContext context = ClusterClientUtil.createBasicAuthContext(Constants.BOSH_AUTH_USERNAME,
                        Constants.BOSH_AUTH_PASSWORD, Constants.BOSH_AUTHHOST);*/
                HttpServletRequest request = ServletActionContext.getRequest();
                HttpSession session  = request.getSession();
                session.setAttribute(USERNAME, userName);
                session.setAttribute(PASSWORD, passwd);
                session.setAttribute(HOST, host);
                session.setAttribute(USERSTATE, ACTIVE);
                /*session.setAttribute(BOSHBASICAUTH, context);
                session.setAttribute(BOSHAUTHHOST, Constants.BOSH_AUTHHOST);*/
                session.setAttribute(CFBASICAUTH, cfContext);
                session.setAttribute(CFAUTHHOST, httpHost);
                dataMap.put("success", true);
                return SUCCESS;
            } else {
                dataMap.put("success", false);
                return "fail";
            }
        } catch (Exception e) {
            logger.error(e.getMessage());;
            e.printStackTrace();
        }
        dataMap.put("success", false);
        return "fail";

    }
    
    public String logout() throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session  = request.getSession();
        session.invalidate();
        dataMap = new HashMap<String, Object>();
        dataMap.put("success", true);
        return SUCCESS;
    }
    
    public String getCurrentUser() throws Exception {
        dataMap = new HashMap<String, Object>();
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session  = request.getSession();
        if (null == session.getAttribute(USERNAME)) {
            dataMap.put("success", false);
            return "fail";
        }
        Map<String, String> response = new HashMap<>();
        response.put(USERNAME, (String) session.getAttribute(USERNAME));
        dataMap.put("data", response);
        dataMap.put("success", true);
        return SUCCESS;
    }

    /**
     * 
     * @return the host
     */
    public String getHost() {
        return host;
    }

    /**
     * 
     * @param host the host to set
     */
    public void setHost(String host) {
        this.host = host;
    }

    /**
     * 
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * 
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * 
     * @return the passwd
     */
    public String getPasswd() {
        return passwd;
    }

    /**
     * 
     * @param passwd the passwd to set
     */
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    /**
     * @return the dataMap
     */
    public Map<String, Object> getDataMap() {
        return dataMap;
    }
}
